package controllers;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import api.controller.IAutoservice;
import api.messages.Messager;
import api.model.IDBManager;
import entitiesfactory.EntitiesFactory;

public class Autoservice implements IAutoservice {

	private IDBManager dbManager;
	private EntitiesFactory entities_factory = EntitiesFactory.getInstance();
	private SimpleDateFormat formatter = new SimpleDateFormat(Messager.DATE_FORMAT);
	private ArrayList<String> exceptionMap = new ArrayList<String>();

	public Autoservice() {
		dbManager = (IDBManager) entities_factory.getObject(IDBManager.class);
	}

	public ArrayList<String> getEmptyGarage() {
		return dbManager.getEmptyGarage();
	}

	public ArrayList<String> SortOrderByBeginningDate() {
		return dbManager.SortOrderByBeginningDate();
	}

	public ArrayList<String> SortOrderByDeadlineDate() {
		return dbManager.SortOrderByDeadlineDate();
	}

	public ArrayList<String> SortOrderByFillingDate() {
		return dbManager.SortOrderByFillingDate();
	}

	public ArrayList<String> SortOrderByPrice() {
		return dbManager.SortOrderByPrice();
	}

	public ArrayList<String> SortMasterByEmployment() {
		return dbManager.SortMasterByEmployment();
	}

	public ArrayList<String> SortMasterByName() {
		return dbManager.SortMasterByName();
	}

	public ArrayList<String> SortMadingOrderByBeginningDate() {
		return dbManager.SortMadingOrderByBeginningDate();

	}

	public ArrayList<String> SortMadingOrderByDeadlineDate() {
		return dbManager.SortMadingOrderByDeadlineDate();
	}

	public ArrayList<String> SortMadingOrderByFillinggDate() {
		return dbManager.SortMadingOrderByFillinggDate();

	}

	public ArrayList<String> SortMadingOrderByPrice() {
		return dbManager.SortMadingOrderByPrice();

	}

	public ArrayList<String> getOrderByMaster(String parameters) {
				return dbManager.getOrdersByMaster(parameters);
	}

	public ArrayList<String> getMasterByOrder(String parameters) {
		Integer numberOrder = Integer.parseInt(parameters);
		return dbManager.getMastersByOrder(numberOrder);
	}

	public ArrayList<String> SortDeletedOrderByPeriodByBeginningDate(
			String parameters) {
		String[] arr = new String[2];
		arr = parameters.split(" ");
		try {
			Date begin = formatter.parse(arr[0]);
			Date end = formatter.parse(arr[1]);
			return dbManager
					.SortDeletedOrderByPeriodByBeginningDate(begin, end);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortCanceledOrderByPeriodByBeginningDate(String parameters) {
		String[] arr = new String[2];
		arr = parameters.split(" ");
		try {
			Date begin = formatter.parse(arr[0]);
			Date end = formatter.parse(arr[1]);		
		return dbManager.SortCanceledOrderByPeriodByBeginningDate(begin, end);
		}catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortMadeOrderByPeriodByBeginningDate(String parameters) {
		String[] arr = new String[2];
		arr = parameters.split(" ");
		try {
			Date begin = formatter.parse(arr[0]);
			Date end = formatter.parse(arr[1]);
		return dbManager.SortMadeOrderByPeriodByBeginningDate(begin, end);
		}catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortDeletedOrderByPeriodByDeadlineDate(String parameters) {
		String[] arr = new String[2];
		arr = parameters.split(" ");
		try {
			Date begin = formatter.parse(arr[0]);
			Date end = formatter.parse(arr[1]);
		return dbManager.SortDeletedOrderByPeriodByDeadlineDate(begin, end);
		}catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortCanceledOrderByPeriodByDeadlineDate(String parameters) {
		String[] arr = new String[2];
		arr = parameters.split(" ");
		try {
			Date begin = formatter.parse(arr[0]);
			Date end = formatter.parse(arr[1]);
		return dbManager.SortCanceledOrderByPeriodByDeadlineDate(begin, end);
		}catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortMadeOrderByPeriodByDeadlineDate(String parameters) {
		String[] arr = new String[2];
		arr = parameters.split(" ");
		try {
			Date begin = formatter.parse(arr[0]);
			Date end = formatter.parse(arr[1]);
		return dbManager.SortMadeOrderByPeriodByDeadlineDate(begin, end);
		}catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortDeletedOrderByPeriodByFillingDate(String parameters) {
		String[] arr = new String[2];
		arr = parameters.split(" ");
		try {
			Date begin = formatter.parse(arr[0]);
			Date end = formatter.parse(arr[1]);
		return dbManager.SortDeletedOrderByPeriodByFillingDate(begin, end);
		}catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortCanceledOrderByPeriodByFillingDate(String parameters) {
		String[] arr = new String[2];
		arr = parameters.split(" ");
		try {
			Date begin = formatter.parse(arr[0]);
			Date end = formatter.parse(arr[1]);
		return dbManager.SortCanceledOrderByPeriodByFillingDate(begin, end);
		}catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortMadeOrderByPeriodByFillingDate(String parameters) {
		String[] arr = new String[2];
		arr = parameters.split(" ");
		try {
			Date begin = formatter.parse(arr[0]);
			Date end = formatter.parse(arr[1]);
		return dbManager.SortMadeOrderByPeriodByFillingDate(begin, end);
		}catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortDeletedOrderByPeriodByPrice(String parameters) {
		String[] arr = new String[2];
		arr = parameters.split(" ");
		try {
			Date begin = formatter.parse(arr[0]);
			Date end = formatter.parse(arr[1]);
		return dbManager.SortDeletedOrderByPeriodByPrice(begin, end);
		}catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
		
	}

	public ArrayList<String> SortCanceledOrderByPeriodByPrice(String parameters) {
		String[] arr = new String[2];
		arr = parameters.split(" ");
		try {
			Date begin = formatter.parse(arr[0]);
			Date end = formatter.parse(arr[1]);
		return dbManager.SortCanceledOrderByPeriodByPrice(begin, end);
		}catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
		
	}

	public ArrayList<String> SortMadeOrderByPeriodByPrice(String parameters) {
		String[] arr = new String[2];
		arr = parameters.split(" ");
		try {
			Date begin = formatter.parse(arr[0]);
			Date end = formatter.parse(arr[1]);
		return dbManager.SortMadeOrderByPeriodByPrice(begin, end);
		}catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
		
	}

	public ArrayList<String> getFreePlacesOnDate(String parameters) {
		try {
		Date date=formatter.parse(parameters);
		return dbManager.getNumberEmptyPlaces(date);
		}catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> getNextFreeDate() {
		return dbManager.getNextFreeDate();
	}

	public ArrayList<String> addMaster(String parameters) {
		return dbManager.addMaster(parameters);
	}

	public ArrayList<String> deleteMaster(String parameters) {
		return dbManager.deleteMaster(parameters);
	}

	public ArrayList<String> addGarage(String parameters) {
		Integer numberGarage=Integer.parseInt(parameters);
		return dbManager.addGarage(numberGarage);
	}

	public ArrayList<String> deleteGarage(String parameters) {
		Integer numberGarage=Integer.parseInt(parameters);
		return dbManager.deleteGarage(numberGarage);
	}

	public ArrayList<String> addOrder(String parameters) {
		String[] array=parameters.split(" ");
		Integer orderNumber=Integer.parseInt(array[0]);
		String masterName=array[1];
		Integer numberGarage=Integer.parseInt(array[2]);
		return dbManager.addOrder(orderNumber, masterName, numberGarage);
	}

	public ArrayList<String> markOrderDeleted(String parameters) {
		Integer numberOrder=Integer.parseInt(parameters);
		return dbManager.markOrderDeleted(numberOrder);
	}

	public ArrayList<String> markOrderCanceled(String parameters) {
		Integer numberOrder=Integer.parseInt(parameters);
		return dbManager.markOrderCanceled(numberOrder);
	}

	public ArrayList<String> markOrderMade(String parameters) {
		Integer numberOrder=Integer.parseInt(parameters);
		return dbManager.markOrderMade(numberOrder);
	}

	public ArrayList<String> ShirtTimeOrder(String parameters) {
		String[] arr=parameters.split(" ");
		try{
		Date newDate=formatter.parse(arr[0]);
		Integer numberOrder=Integer.parseInt(arr[1]);
		return dbManager.ShirtTimeOrder(newDate, numberOrder);
		}catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> cloneOrder(String parameters) {
		Integer numberOrder=Integer.parseInt(parameters);
		return dbManager.cloneOrder(numberOrder);
	}

	public ArrayList<String> exportGarages() {
		return dbManager.exportGarages();

	}

	public ArrayList<String> exportMasters() {
		return dbManager.exportMasters();

	}

	public ArrayList<String> exportOrders() {
		return dbManager.exportOrders();
	}

	public ArrayList<String> importGarages() {
		return dbManager.importGarages();
	}

	public ArrayList<String> importMasters() {
		return dbManager.importMasters();
	}

	public ArrayList<String> importOrders() {
		return dbManager.importOrders();
	}

	public ArrayList<String> resaveData() {
		return dbManager.saveData();
	}

}
